package com.jemmyee.pizza.web.controller;

import javax.servlet.http.HttpServletRequest;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.jemmyee.core.dao.Page;
import com.jemmyee.core.utils.DateUtils;
import com.jemmyee.pizza.entity.PreExcerpt;
import com.jemmyee.pizza.service.IPreExcerptService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.PreExcerptBean;

/**
 * @author qiuchao
 * 2010-12-21
 */
@Controller
@RequestMapping("/admin/pre_excerpt")
public class PreExcerptController {
	@Autowired
	private IPreExcerptService<PreExcerpt> preExcerptService;
	
	
	
	@RequestMapping("/list/{from}/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute PreExcerptBean bean,HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(PreExcerpt.class);
		PreExcerptBean preExcerptBean = new PreExcerptBean();
		if (from.equals("form")) {
			preExcerptBean = bean;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, preExcerptBean);
		}
		if (from.equals("page")) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				preExcerptBean = (PreExcerptBean) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
		}
		if (from.equals("menu")) {
			System.out.println("menu is call");
		}
		
		if(preExcerptBean.getName() != null && !preExcerptBean.getName().equals("")){
			dc.add(Restrictions.like("u_name", preExcerptBean.getName(), MatchMode.ANYWHERE));
		}
		
		if(preExcerptBean.getNumber() != null && !preExcerptBean.getNumber().equals("")){
			dc.add(Restrictions.like("u_id", preExcerptBean.getNumber(), MatchMode.ANYWHERE));
		}
		
		if(preExcerptBean.getType() != null && !preExcerptBean.getType().equals("")){
			dc.add(Restrictions.eq("s_from", preExcerptBean.getType()));
		}
		
		if(preExcerptBean.getStatus() != null && !preExcerptBean.getStatus().equals("")){
			dc.add(Restrictions.eq("payStatus", preExcerptBean.getStatus()));
		}
		// 发布时间
		if (preExcerptBean.getStartTime() != null && !preExcerptBean.getStartTime().equals("")) {
			dc.add(Restrictions.ge("s_date", preExcerptBean.getStartTime()));
			System.out.println(DateUtils.formatDateTime(DateUtils.parseDateToString(preExcerptBean.getStartTime())));
		}
		if (preExcerptBean.getEndTime() != null && !preExcerptBean.getEndTime().equals("")) {
			dc.add(Restrictions.le("s_date", preExcerptBean.getEndTime()));
		}

//		dc.addOrder(Order.desc("excerptTime"));
		ModelAndView mv = new ModelAndView();
		Page page = preExcerptService.findPageByCriteria(dc,Constants.DEFAULT_PAGESIZE, startIndex,"s_date");
		
		
		mv.addObject("page", page);
		mv.addObject("preExcerptBean", preExcerptBean);
		mv.setViewName("/admin/pre_excerpt/list");
		return mv;
	}
	
	
}
